//
//  UploadFile.m
//  MobileOA
//
//  Created by liqing-mac on 14-2-28.
//  Copyright (c) 2014年 chenxiaojuan. All rights reserved.
//
//
//


#import "UploadFile.h"
#import "AFHTTPClient.h"
#import "AFHTTPRequestOperation.h"
#import "AFJSONRequestOperation.h"

@implementation UploadFile



+(void)uploadFileWithFilePath:(NSString *)filePath servicePath:(NSString*)serPath params:(NSDictionary *) params calback:(CallbackBlock)mcCalback{
    NSString *path = UPLOADFILEPATH;
    
    NSURL *baseURL = [NSURL URLWithString:UPLOADBASEURL];

    NSData *data = [NSData dataWithContentsOfFile:filePath];
    
    NSString *servicePath = [NSString stringWithFormat:@"%@/%@",serPath,[params objectForKey:@"filename"]];

    NSDictionary *parameters = @{@"SavePath":servicePath,@"ClientKey":@"1",@"ClientName":@"mobile_SC"};
    
    AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
    NSMutableURLRequest *afRequest = [client multipartFormRequestWithMethod:@"POST"
                                                                       path:path
                                                                 parameters:parameters
                                                  constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                                                      
                                                      [formData appendPartWithFileData:data
                                                                                  name:@"name"
                                                                              fileName:@"fileName"
                                                                              mimeType:@""];
//                                                                              mimeType:@"audio/mpeg3"];
                                                      
                                                  }];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:afRequest];
    
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSLog(@"成功呢 ===%@",operation.responseString);
         
         if(mcCalback){
             if (operation.responseString) {
                 NSDictionary *returnData = [operation.responseString objectFromJSONString];
                 if([@"SUCCEED" isEqualToString:[returnData objectForKey:@"STATUS"]])
                 {
                     NSMutableDictionary * returnParams = [[NSMutableDictionary alloc] initWithDictionary:params];
                     [returnParams setObject:[returnData objectForKey:@"URL"] forKey:@"url"];
                     mcCalback(YES,1,@"上传成功",returnData,params);
                 }
                 else
                 {
                     mcCalback(NO,-1,@"上传失败",nil,params);
                 }
            }else{
                mcCalback(NO,-1,@"上传失败",nil,params);
            }
         }
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"Failure: %@", error);
         
         if(mcCalback){
             
             mcCalback(NO,-1,@"上传失败",nil,params);
             
         }
     }];
    
    [operation start];

    
}


@end






























